package com.laotou99.wmsjjj.wmsjjj.service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.laotou99.wmsjjj.wmsjjj.bean.po.AUTOPO;
import com.laotou99.wmsjjj.wmsjjj.repository.AUTORepository;

@Service("autoService")
public class AUTOService {

	@Autowired
	AUTORepository autoRepository;

	public String showList(JSONObject jo1) {
		String result  = "";
		List<AUTOPO> findAll = autoRepository.findAll();
		Collections.reverse(findAll);
		result = JSON.toJSONString(findAll);
		System.out.println("result-=>"+result);
		return result;
	}

	public String addNew(JSONObject jo1) {
		System.out.println("jo1-=>"+jo1.toJSONString());
		
		AUTOPO autopo = jo1.toJavaObject(AUTOPO.class);
		autopo.setCreateDate(new Date());
		autopo.setModifyDate(new Date());
		
		AUTOPO autopo2 = autoRepository.save(autopo);
		System.out.println("autopo2-=>"+JSON.toJSONString(autopo2));
		return "OK";
	}

	public String selectById(JSONObject jo1) { 
		System.out.println("jo1-=>"+jo1.toJSONString());
		Long lng1 = jo1.getLong("id");
		
		AUTOPO findOne = autoRepository.findOne(lng1);
		return JSON.toJSONString(findOne);
	}

	public String update(JSONObject jo1) {
		System.out.println("update jo1-=>"+jo1.toJSONString()); 
		
		AUTOPO ap1 = jo1.toJavaObject(AUTOPO.class);
		ap1.setModifyDate(new Date());
		AUTOPO ap2 = autoRepository.save(ap1);
		System.out.println("ap2-=>"+JSON.toJSONString(ap2));
		return "OK";
	}

	public String delete(JSONObject jo1) {
		System.out.println("jo1"+jo1.toJSONString()); 
		Long lng1 = jo1.getLong("id");
		autoRepository.delete(lng1);
		return "OK";
	}
	
	
}
